package cn.edu.hy.demo10.controller;

import cn.edu.hy.demo10.entity.Dog;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/ajax")
public class AjaxController {


    @GetMapping(value = "index")
    public String index(Model model, ModelMap modelMap) {

        model.addAttribute("title1", "testModel");
        modelMap.put("title2", "testModelMap");

        return "demo10/index";
    }

    /**
     * ajax 测试 String返回类型
     */
    @RequestMapping(method = RequestMethod.GET,value = "findName")
    @ResponseBody
    public String findName(){

        return "ajax";
    }

    /**
     * ajax 测试 对象返回类型
     */
    @RequestMapping(value = "findStudent")
    @ResponseBody
    public Dog findStudent(){

        Dog student = new Dog();
        student.setAge(17);
        student.setNo("001");
        student.setName("max");
        
        return student;
    } 
    
    
    
    /**
     * ajax 测试 list返回类型
     */
    @RequestMapping(value = "findStudents")
    @ResponseBody
    public List<Dog> findStudents(){

        return getList();
    }

    private static List<Dog> getList() {
        List<Dog> students = new ArrayList<Dog>();

        Dog student = new Dog();
        student.setAge(17);
        student.setNo("001");
        student.setName("max");

        Dog student1 = new Dog();
        student1.setAge(18);
        student1.setNo("002");
        student1.setName("little");

        students.add(student);
        students.add(student1);
        return students;
    }


    /**
     * ajax 测试 map返回类型
     */
    @RequestMapping(value = "findMap")
    @ResponseBody
    public Map<String,Object> findMap(){
            Map<String,Object>  map = new HashMap<String,Object>();
            map.put("name","mapName");
            map.put("age","16");

            map.put("list",getList());
            return map;
    }
    
    
    
}
